/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;
/**
 * 说明：
 * 操作日志管理
 * @Author：
 * @Date：2012-03-21
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TOpLog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.SystemManageLogModel;
import com.ruipengkj.service.TOpLogService;
import com.ruipengkj.view.base.BasePagePanel;

public class SystemManageLog  extends BasePagePanel<TOpLog> implements Runnable{
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent  employeesName;//员工姓名
	private JLabelJTextFieldComponent  actiontype;//操作类型
	private JButton selectButton;//查询
	private JTable table;
	private JScrollPane tableJsp;
	private SystemManageLogModel systemManageLogModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TOpLog> pageView;//翻页主键
	
	private TOpLogService tOpLogService;//操作日记管理
	public SystemManageLog() {
		tOpLogService=BuildSpringApplication.getInstance().getTOpLogServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		pageView=new PageView<TOpLog>();
		
		systemManageLogModel = new SystemManageLogModel();
		table=uiFactory.createRPJTable(systemManageLogModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(pagingComponent, BorderLayout.SOUTH);
	}
	
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("操作日记",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_1));
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		employeesName = new JLabelJTextFieldComponent("员工信息：", 30);
		actiontype = new JLabelJTextFieldComponent("操作类型：", 30);
		selectButton = new RPJButtonBlue("查询");
		top_panel.add(actiontype);
		top_panel.add(employeesName);
		top_panel.add(selectButton);
		queryPanel.addPanel(top_panel);
//		queryPanel.addBoxPanel(top_panel);
		return queryPanel;
	}
	
	private void initListener(){
		selectButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==selectButton){
			termQuery(1);
		}
	}
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TOpLog opLog=new TOpLog();
		TEmployee employee=new TEmployee();
		String employeestr=employeesName.getText().getText().trim();
		if(employeestr.length()==8){
			employee.setEmployeeSn(employeestr);
		}
		else if(employeestr.length()==12){
			employee.setCardNum(employeestr);
		}
		else{
			employee.setName(employeestr);
		}
		opLog.setTEmployee(employee);
		opLog.setOperatestr(actiontype.getText().getText().trim());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TOpLog> pageModel = tOpLogService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),opLog);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						systemManageLogModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						systemManageLogModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TOpLog> getPageView() {
		return pageView;
	}

	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void clear() {
		systemManageLogModel.clear();
		employeesName.getText().setText("");
		
	}
}
